package com.yu.myiptv;

import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class ChannelActivity extends AppCompatActivity {
    private RecyclerView channelView;
    private ChannelAdapter mAdapter;
    private final List<Channel> mChannels = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_channel);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.channel_list), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        // 初始化RecyclerView
        channelView = findViewById(R.id.channel_list);
        channelView.setLayoutManager(new LinearLayoutManager(this));

        // 添加频道数据
        addChannels();

        mAdapter = new ChannelAdapter(mChannels, this);
        channelView.setAdapter(mAdapter);

        // 设置RecyclerView的点击事件监听器
        channelView.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() {
            public boolean onInterceptTouchEvent(RecyclerView rv, int action) {
                return false;
            }

            @Override
            public boolean onInterceptTouchEvent(@NonNull RecyclerView rv, @NonNull MotionEvent e) {
                return false;
            }

            @Override
            public void onTouchEvent(@NonNull RecyclerView rv, @NonNull MotionEvent event) {
                View view = Objects.requireNonNull(rv.getLayoutManager()).findViewByPosition(event.getActionIndex());
                int position = channelView.getChildAdapterPosition(view);
                mAdapter.setmSelectedPosition(position);
                mAdapter.notifyDataSetChanged();
            }

            @Override
            public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {

            }
        });
    }

    private void addChannels() {
        mChannels.add(new Channel("1", "Channel 1", ""));
    }

}